package data;

import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;

import entity.User;

public class UserDao {
	public static User getUser(String name,String pass){
		User user = null;
		if (name !=null||pass!=null) {
			String sql = "select * from user where name = '"+name+"' and pass = '"+pass+"'";
			ResultSet rs = BaseDao.executeQuery(sql);
			try{
				if (rs.next()) {
					user=new User();
					user.setId(rs.getInt("id"));
				    user.setName(ChangeString.ISOToGBK(rs.getString("name")));
				    user.setPass(ChangeString.ISOToGBK(rs.getString("pass")));
				    user.setIs_admin(rs.getByte("is_admin"));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
 return user;
}
	public static User getUser(int id){
		User user = null;
		if (id>0){
			String sql = "select*from user where name="+id;
			ResultSet rs = BaseDao.executeQuery(sql);
			try{
				if (rs.next()) {
					user=new User();
					user.setId(rs.getInt("id"));
				    user.setName(ChangeString.ISOToGBK(rs.getString("name")));
				    user.setPass(ChangeString.ISOToGBK(rs.getString("pass")));
				    user.setIs_admin(rs.getByte("is_admin"));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
 return user;
}
	public static User getUser(String name){
		User user = null;
		if (name != null){
			String sql = "select*from user where name='"+name+"'";
			ResultSet rs = BaseDao.executeQuery(sql);
			try{
				if (rs.next()) {
					user=new User();
					user.setId(rs.getInt("id"));
				    user.setName(ChangeString.ISOToGBK(rs.getString("name")));
				    user.setPass(ChangeString.ISOToGBK(rs.getString("pass")));
				    user.setIs_admin(rs.getByte("is_admin"));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
 return user;
}

	public static List<User> selectUserList(){
		User user= null;
		List<User> list = new ArrayList<User>();
		String sql = "select*from user";
		ResultSet rs = BaseDao.executeQuery(sql);
		try{
		while(rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
			    user.setName(ChangeString.ISOToGBK(rs.getString("name")));
			    user.setPass(ChangeString.ISOToGBK(rs.getString("pass")));
			    user.setIs_admin(rs.getByte("is_admin"));
			    list.add(user);
		}
	}catch (SQLException e) {
		e.printStackTrace();
	
	}
   return list;
}

   public static int insertUser(User user){
	   if(user!=null){
		   int id = user.getId();
		   String name = user.getName();
		   String pass = user.getPass();
		   byte is_admin = user.getIs_admin();
		   String sql = "insert into user values("+id+",'"+name+"','"+pass+"',"+is_admin+")";
		   return BaseDao.executeUpdate(sql);
	   }else {
		return -1;
	}
   }
   public static int updateUser(User user){
	   if(user!=null){
		   int id = user.getId();
		   String name = user.getName();
		   String pass = user.getPass();
		   byte is_admin = user.getIs_admin();
		   String sql = "update user set name='"+name+"',pass='"+pass+"',is_admin="+is_admin+" where id="+id;
		   return BaseDao.executeUpdate(sql);
	   }else {
		return -1;
	}
   }
   public static int updatePass(String name,String pass){
	   if (name !=null&&pass !=null){
		   String sql = "update user set pass='"+pass+"'where name='"+name+"'";
		   return BaseDao.executeUpdate(sql);
	   }else {
		return -1;
	}
   }
   public static int deleteUser(int id) {
		String sql = "delete from user  where id=" + id ;
		return BaseDao.executeUpdate(sql); 
	}

	/** 成员方法9：清空所有用户信息 */
	public static void emptyUser() {
		String sql = "delete from user";
		BaseDao.executeUpdate(sql); 
	
}
}
	
   